package io.virtualapp.hook_sdk.dao;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import android.util.Log;
import com.lody.virtual.client.core.VirtualCore;
import java.util.ArrayList;
import java.util.List;

import io.virtualapp.hook_sdk.firewall.FireWallType;
import io.virtualapp.hook_sdk.firewall.UrlBean;
import io.virtualapp.hook_sdk.models.InstallAppBean;
import io.virtualapp.hook_sdk.models.NotesBean;

public class RiNotesService {

    private RiNotesHelper helper;
    private RiNotesService(){
        helper = new RiNotesHelper(VirtualCore.get().getContext());
    }

    public String app_name = "";

    private volatile static RiNotesService instance;

    public static RiNotesService getInstance(){
        if (instance == null) {
            synchronized (RiNotesService.class) {
                if (instance == null) {
                    instance = new RiNotesService();
                }
            }
        }
        return instance;
    }

    public boolean insertApp(InstallAppBean bean){
        if (bean == null)
            return false;
        if (TextUtils.isEmpty(bean.app_name))
            return false;
        if (bean.app_time == 0)
            return false;
        ContentValues values = new ContentValues();
        values.put("app_name",bean.app_name);
        values.put("app_time",bean.app_time);
        values.put("apk_path",bean.apk_path);
        return helper.insert(values,"app_table");
    }

    public boolean insertesApp(List<InstallAppBean>list){
        if (list == null)
            return false;
        if (list.size() == 0)
            return false;
        List<ContentValues>valuesArrayList = new ArrayList<>();
        for (InstallAppBean bean:list){
            ContentValues values = new ContentValues();
            values.put("app_name",bean.app_name);
            values.put("app_time",bean.app_time);
            valuesArrayList.add(values);
        }
        return helper.insertes(valuesArrayList,"app_table");
    }

    public boolean insertNotes(NotesBean bean){
        if (bean == null)
            return false;
        if (TextUtils.isEmpty(bean.app_interface))
            return false;
        ContentValues values = new ContentValues();
        values.put("app_id",bean.app_id);
        values.put("app_adopt",bean.app_adopt);
        values.put("app_interface",bean.app_interface);
        values.put("interface_type",bean.interface_type);
        return helper.insert(values,"notes_table");
    }

    public boolean insertesNotes(List<NotesBean>list){
        if (list == null)
            return false;
        if (list.size() == 0)
            return false;
        List<ContentValues>valuesArrayList = new ArrayList<>();
        for (NotesBean bean:list){
            ContentValues values = new ContentValues();
            values.put("app_id",bean.app_id);
            values.put("app_adopt",bean.app_adopt);
            values.put("app_interface",bean.app_interface);
            values.put("interface_type",bean.interface_type);
            valuesArrayList.add(values);
        }
        return helper.insertes(valuesArrayList,"notes_table");
    }

    public int selApp(String appName){
        if (TextUtils.isEmpty(appName))
            return -1;
        return helper.selApp(appName);
    }

    public boolean delApp(String appName){
        if (TextUtils.isEmpty(appName))
            return false;
        return helper.delApps(appName);
    }

    public List<InstallAppBean> selAllApp(){
        return helper.selApp();
    }
    public  List<NotesBean> selNotes(){
        return helper.selNotes();
    }

    public void insertUrl(String url){
        ContentValues values = new ContentValues();
        values.put("hook_url",url);
        helper.insert(values,"url_table");
    }

    public void insertType(FireWallType type){
        int index = 0;
        if (type == FireWallType.NONE)
            index = 0;
        if (type == FireWallType.WHITE)
            index = 1;
        if (type == FireWallType.BLACK)
            index = 2;
        helper.delType();
        ContentValues values = new ContentValues();
        values.put("type",index);
        helper.insert(values,"type_table");
    }

    public  List<UrlBean>selUrl(){
        return helper.selUrl();
    }

    public FireWallType selType(){
        return helper.selType();
    }

    public void delUrl (int id){
        helper.delUrl(id);
    }

    public void insertWhite(String str){
        helper.delWhite();
        ContentValues values = new ContentValues();
        values.put("whiteStr",str);
        helper.insert(values,"white_table");
    }

    public String selWhite(){
        return helper.selWhite();
    }

}

